<template>
    <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
        <el-form :model="addPanFileSharesForm" ref="addPanFileSharesRef" :rules="addPanFileSharesrules">
                        <el-form-item label="文件ID" :label-width="formLabelWidth"
                                      prop="fileId" v-if="addPanFileSharesForm.id">
                            <el-input
                                    v-model="addPanFileSharesForm.fileId"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写文件ID"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="文件夹ID" :label-width="formLabelWidth"
                                      prop="folderId" v-if="addPanFileSharesForm.id">
                            <el-input
                                    v-model="addPanFileSharesForm.folderId"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写文件夹ID"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="共享代码" :label-width="formLabelWidth"
                                      prop="shareCode">
                            <el-input
                                    v-model="addPanFileSharesForm.shareCode"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写共享代码"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="共享链接" :label-width="formLabelWidth"
                                      prop="shareUrl">
                            <el-input
                                    v-model="addPanFileSharesForm.shareUrl"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写共享链接"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="有效期（小时）" :label-width="formLabelWidth"
                                      prop="validityPeriod" v-if="addPanFileSharesForm.id">
                            <el-input
                                    v-model="addPanFileSharesForm.validityPeriod"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写有效期（小时）"
                            ></el-input>
                        </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="save()">保存</el-button>
      </span>
        </template>
    </el-dialog>
</template>

<script>
    import {ref} from "vue";
    import {httpService} from "@/utils/httpService";
    import {ElMessage, ElForm} from "element-plus";
    import {vxRule} from "@/utils/validator";

    export default {
        name: "AddPanFileShares",
        components: {ElForm, ElMessage},
        data() {
            return {
                dialogFormVisible: ref(false),
                formLabelWidth: "120px",
                title: "",
                addPanFileSharesForm: {
                    fileId: "",
                    folderId: "",
                    shareCode: "",
                    shareUrl: "",
                    validityPeriod: "",
                },
                // 校验规则
                addPanFileSharesrules: {
                    shareCode: vxRule(true, null, "blur"),
                    shareUrl: vxRule(true, null, "blur"),
                },
            };
        },
        methods: {
            closeDialog() {
                this.clear();
            },
            cancel() {
                this.dialogFormVisible = false;
                this.clear();
            },
            getPanFileSharesById(id) {
                httpService("/javaApi/panFileShares/queryById", {id: id}, "get").then(
                    (data) => {
                        this.addPanFileSharesForm = data;
                    }
                );
            },
            clear() {
                this.dialogFormVisible = false;
                //清空form
                    this.addPanFileSharesForm.id = "";
                    this.addPanFileSharesForm.fileId = "";
                    this.addPanFileSharesForm.folderId = "";
                    this.addPanFileSharesForm.shareCode = "";
                    this.addPanFileSharesForm.shareUrl = "";
                    this.addPanFileSharesForm.validityPeriod = "";
            },
            save() {
                this.$refs.addPanFileSharesRef.validate((valid) => {
                    if (valid) {
                        let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
                        params.append("fileId", this.addPanFileSharesForm.fileId);
                        params.append("folderId", this.addPanFileSharesForm.folderId);
                        params.append("shareCode", this.addPanFileSharesForm.shareCode);
                        params.append("shareUrl", this.addPanFileSharesForm.shareUrl);
                        params.append("validityPeriod", this.addPanFileSharesForm.validityPeriod);
                        if (this.addPanFileSharesForm.id) {
                            params.append("id", this.addPanFileSharesForm.id);
                            httpService("/javaApi/panFileShares/edit", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success(data.message);
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getPanFileSharesList();
                                    } else {
                                        ElMessage.error(data.message);
                                    }
                                }
                            );
                        } else {
                            httpService("/javaApi/panFileShares/add", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success(data.message);
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getPanFileSharesList();
                                    } else {
                                        ElMessage.error(data.message);
                                    }
                                }
                            );
                        }
                    } else {
                        return false;
                    }
                });
            },
        },
    };
</script>
<style>
</style>